package zzs.generalframework.office.core.excel.sheet;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @title
 * @description
 * @project zzs-office
 * @package zzs.generalframework.office.core.excel.sheet
 * @author zzs
 * @date 2018年1月30日
 * @version 1.0
 */
public class SheetContext
{

	private Logger logger = LoggerFactory.getLogger(SheetContext.class);

	/**
	 * 创建sheet页
	 * 
	 * @param workbook
	 * @return
	 */
	public Sheet createSheet(Workbook workbook)
	{
		logger.info("====创建Sheet对象开始====");
		logger.debug("====创建Sheet对象,入参:Workbook=[{}]====", workbook);
		Sheet sheet = workbook.createSheet();
		logger.debug("====创建Sheet对象,出参:Sheet=[{}]====", sheet);
		logger.info("====创建Sheet对象结束====");
		return sheet;
	}

	/**
	 * 创建sheet页
	 * 
	 * @param workbook
	 * @param sheetName
	 * @return
	 */
	public Sheet createSheet(Workbook workbook, String sheetName)
	{
		logger.info("====创建Sheet对象开始====");
		logger.debug("====创建Sheet对象,入参:Workbook=[{}],String=[{}]====", workbook, sheetName);
		Sheet sheet = workbook.createSheet(sheetName);
		logger.debug("====创建Sheet对象,出参:Sheet=[{}]====", sheet);
		logger.info("====创建Sheet对象结束====");
		return sheet;
	}

	/**
	 * 获取指定的Sheet
	 * 
	 * @param workbook
	 * @param sheetNum
	 * @return
	 */
	public Sheet getSheet(Workbook workbook, int sheetNum)
	{
		logger.info("====获取Sheet对象开始====");
		logger.debug("====获取Sheet对象,入参:Workbook=[{}],int=[{}]====", workbook, sheetNum);
		Sheet sheet = workbook.getSheetAt(sheetNum);
		logger.debug("====获取Sheet对象,出参:Sheet=[{}]====", sheet);
		logger.info("====获取Sheet对象结束====");
		return sheet;
	}

	/**
	 * 获取指定的Sheet
	 * 
	 * @param workbook
	 * @param sheetName
	 * @return
	 */
	public Sheet getSheet(Workbook workbook, String sheetName)
	{
		logger.info("====获取Sheet对象开始====");
		logger.debug("====获取Sheet对象,入参:Workbook=[{}],String=[{}]====", workbook, sheetName);
		Sheet sheet = workbook.getSheet(sheetName);
		logger.debug("====获取Sheet对象,出参:Sheet=[{}]====", sheet);
		logger.info("====获取Sheet对象结束====");
		return sheet;
	}

	/**
	 * 获取Sheet页名称
	 * 
	 * @param sheet
	 * @return
	 */
	public String getSheetName(Sheet sheet)
	{
		logger.info("====获取Sheet名称开始====");
		logger.debug("====获取Sheet名称,入参:Sheet=[{}]====", sheet);
		String sheetName = sheet.getSheetName();
		logger.debug("====获取Sheet名称,出参:String=[{}]====", sheetName);
		logger.info("====获取Sheet名称结束====");
		return sheetName;
	}

	/**
	 * getLastRowNum<br>
	 * 如果sheet中一行数据都没有则返回-1，只有第一行有数据则返回0，最后有数据的行是第n行则返回 n-1；<br>
	 * getLastCellNum<br>
	 * 如果 row中一列数据都没有则返回-1，只有第一列有数据则返回1，最后有数据的列是第n列则返回 n；<br>
	 * getPhysicalNumberOfRows<br>
	 * 获取有记录的行数，即：最后有数据的行是第n行，前面有m行是空行没数据，则返回n-m；<br>
	 * getPhysicalNumberOfCells<br>
	 * 获取有记录的列数，即：最后有数据的列是第n列，前面有m列是空列没数据，则返回n-m；<br>
	 */
	/**
	 * 获取Sheet页行数
	 * 
	 * @param sheet
	 * @return 返回－1,说明为空。第n行返回n-1
	 */
	public int getRowNum(Sheet sheet)
	{
		logger.info("====获取Sheet页行数开始====");
		logger.debug("====获取Sheet页行数,入参:Sheet=[{}]====", sheet);
		int rowSize = sheet.getLastRowNum();
		logger.debug("====获取Sheet页行数,出参:int=[{}]====", rowSize);
		logger.info("====获取Sheet页行数结束====");
		return rowSize;
	}

}
